package com.eclink.asset.consume.controller;

import com.eclink.asset.consume.domain.dao.ConsumeFile;
import com.eclink.asset.consume.domain.vo.ConsumeFileVO;
import com.eclink.asset.pojo.query.AssetItemQuery;
import com.eclink.asset.consume.convert.ConsumeFileConvert;
import com.eclink.asset.consume.service.ConsumeFileService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 耗材清单表Controller
 *
 * @author chenshixian
 * @date 2024-05-21
 */
@Api("耗材档案接口")
@RestController
@RequestMapping("/consume/file")
public class ConsumeFileController extends BaseController {

    @Resource
    private ConsumeFileService service;

    @Resource
    private ConsumeFileConvert convert;

    @ApiOperation("查询列表")
    @PostMapping("/list")
    public ResponseEntity<Page<ConsumeFile>> list(@RequestBody AssetItemQuery query, Pageable page) {
        List<ConsumeFile> list = service.selectList(query, page);
        return ResponseEntity.ok(new PageImpl<>(list, page, ((com.github.pagehelper.Page)list).getTotal()));
    }

    @ApiOperation("导出")
    @GetMapping("/export")
    public ResponseEntity<String> export(AssetItemQuery query) {
        List<ConsumeFile> list = service.selectList(query, null);
        ExcelUtil<ConsumeFileVO> util = new ExcelUtil<>(ConsumeFileVO.class);
        return ResponseEntity.ok(util.writeExcel(convert.dos2vos(list), "导出"));
    }

    @ApiOperation("获取详细信息")
    @GetMapping(value = "/{id}")
    public ResponseEntity<ConsumeFile> getInfo(@PathVariable("id") Long id) {
        ConsumeFile res = service.selectById(id);

        return ResponseEntity.ok(res);
    }

    @ApiOperation("新增")
    @PostMapping
    public ResponseEntity<Integer> add(@RequestBody ConsumeFile assetItem) {
        return ResponseEntity.ok(service.insert(assetItem));
    }

    @ApiOperation("修改")
    @PutMapping
    public ResponseEntity<Integer> edit(@RequestBody ConsumeFile assetItem) {
        return ResponseEntity.ok(service.update(assetItem));
    }


    @ApiOperation("删除")
	@DeleteMapping("/{ids}")
    public ResponseEntity<Integer> remove(@PathVariable Long[] ids) {
        return ResponseEntity.ok(service.deleteByIds(ids));
    }


}
